#include<iostream>
#include<unistd.h>
#include <fcntl.h>
using namespace std;
void SetNonBlock(int fd)
{
    int flags=fcntl(fd,F_GETFL);
    if(flags==-1)
    {
        cerr<<"获取flags失败"<<endl;
        exit(0);
    }
    fcntl(fd,F_SETFL,flags|O_NONBLOCK);
}
int main()
{
    SetNonBlock(0);
    while(true)
    {
        char buf[1024]={0};
        int n=read(0,buf,sizeof(buf)-1);
        if(n>0)
        {
            cout<<"echo# "<<buf<<endl;
        }
        else if(n==0)//读到文件末尾
        {
            break;
        }
        else
        {
            if(errno==EAGAIN)
            {
                cout<<"数据未就绪"<<endl;
                //todo other things
            }
            else
            {
                cout<<"读取错误"<<endl;
                break;    
            }
        }
        sleep(1);
    }
    return 0;
}